package com.aliyun.utils;


import com.aliyun.properties.JwtProperties;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.SignatureException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class JwtUtils {

    @Autowired
    private JwtProperties jwtProperties;


    public static String getJwt(Map map,String key,Integer ttl){
        //map.put("id",1);
        String jwt= Jwts.builder()
                .setClaims(map)
                .signWith(SignatureAlgorithm.HS256, key)
                .setExpiration(new Date(System.currentTimeMillis()+ttl))
                .compact();
        System.out.println(jwt);
        return jwt;
    }
    public static void checkJwt(String jwt,String key){
        try {
            Map map= Jwts.parser()
                    .setSigningKey(key)
                    .parseClaimsJws(jwt).getBody();
            System.out.println(map);
        }catch (SignatureException e){
            System.out.println("签名错误");
        }catch (ExpiredJwtException e){
            System.out.println("jwt校验失败,jwt过期失效");
    }
    }
}
